package io.gitee.tuo_tuo.jfinaldevassistance.db;

import com.jfinal.plugin.activerecord.Model;

/**
 * 类名称：外键约束
 * 描述:
 * 版权: Copyright (c) 2018
 * 作者: tuotuo
 * 版本: 1.0
 * 创建日期: 2018/8/1
 * 创建时间: 13:39
 */
public class ForeignKeyConstraint {

	public String key;
	public Class<? extends Model> modelClass;
	public String foreignKey;
	public Class<? extends Model> foreignModelClass;
	public Relation relation;

	public ForeignKeyConstraint(Class<? extends Model> modelClass, String key,
								Class<? extends Model> foreignModelClass, String foreignKey, Relation relation){
		this.modelClass = modelClass;
		this.key = key;
		this.foreignModelClass = foreignModelClass;
		this.foreignKey = foreignKey;
		this.relation = relation;
	}

	public enum Relation{
		unknown, oneVSone, oneVSmore, moreVSmore;
	}

	@Override
	public int hashCode() {
		int keyHashCode = key == null ? 0 : key.hashCode();
		int foreignKeyHashCode = foreignKey == null ? 0 : foreignKey.hashCode();
		int modelClassHashCode = modelClass == null ? 0 : modelClass.hashCode();
		int foreignModelClassHashCode = foreignModelClass == null ? 0 : foreignModelClass.hashCode();
		return (keyHashCode + foreignKeyHashCode) ^ (modelClassHashCode + foreignModelClassHashCode);
	}

	@Override
	public boolean equals(Object obj) {
		if(this == obj){
			return true;
		}
		if(obj instanceof ForeignKeyConstraint == false){
			return false;
		}
		if(this.hashCode() != obj.hashCode()){
			return false;
		}
		ForeignKeyConstraint constraint = (ForeignKeyConstraint) obj;
		if(this.modelClass.equals(constraint.modelClass) || this.modelClass.equals(constraint.foreignModelClass)){
			if(this.foreignModelClass.equals(constraint.modelClass) || this.foreignModelClass.equals(constraint.foreignModelClass)){
				if(this.key.equals(constraint.key) || this.key.equals(constraint.foreignKey)){
					if(this.foreignKey.equals(constraint.key) || this.foreignKey.equals(constraint.foreignKey)){
						return true;
					}
				}
			}
		}
		return false;
	}

}
